import os
import xml.dom.minidom as xmldom

import cv2

if __name__ == '__main__':
    '''
    展示labelImg 的结果
    '''
    # rootPath = "../validatecode_dec/gowhere_validate"

    rootPath = "/Users/wangzy/study/darknet/darknet-master/scripts/VOCdevkit/VOC2018/JPEGImages"


    files = os.listdir(rootPath)
    windowName = "img"

    for file in files:
        if file.endswith(".xml"):
            xmlfname = os.path.join(rootPath, file)
            fname = os.path.join(rootPath, file.replace(".xml", ".jpg"))
            domobj = xmldom.parse(xmlfname)
            elementobjs = domobj.documentElement

            subobjs = elementobjs.getElementsByTagName("object")

            img = cv2.imread(fname)
            for subobj in subobjs:
                name = subobj.getElementsByTagName("name")[0].firstChild.data
                bndbox = subobj.getElementsByTagName("bndbox")[0]

                xmin = int(bndbox.getElementsByTagName("xmin")[0].firstChild.data)
                ymin = int(bndbox.getElementsByTagName("ymin")[0].firstChild.data)
                xmax = int(bndbox.getElementsByTagName("xmax")[0].firstChild.data)
                ymax = int(bndbox.getElementsByTagName("ymax")[0].firstChild.data)
                print(name, xmin, ymin, xmax, ymax)

                cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (0, 0, 255), thickness=1)

            cv2.imshow(windowName, img)
            cv2.resizeWindow(windowName, 600, 600)

            cv2.waitKey(1000)

    cv2.destroyAllWindows()
    exit(0)
